package com.woniuxy.stage3demo.controller.chenyuxuanController;


import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.EntranceRuleBean;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.chenyxuanService.EntranceRuleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 准入规则方法controller
 */
@RequestMapping("/entranceRule")
@RestController
public class EntranceRuleController {

    @Resource
    private EntranceRuleService entranceRuleServiceImpl;

    /**
     * 分页查询
     * @param pageRequest
     * @param entranceRuleBean
     * @return
     */
    @RequestMapping(value = "/page", produces = {"application/json;charset=utf-8"})
    public TableResult<EntranceRuleBean> findAllDept(PageRequest pageRequest, EntranceRuleBean entranceRuleBean) {
        System.out.println(pageRequest);
        System.out.println(entranceRuleBean);
        PageInfo<EntranceRuleBean> pageInfo=entranceRuleServiceImpl.findAllEntranceRule(pageRequest,entranceRuleBean);
        System.out.println(pageInfo);
        return new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加准则配置
     * @param entranceRuleBean
     * @return
     */
    @PostMapping(value = "/add", produces = {"application/json;charset=utf-8"})
    public ResultMsg addEntranceRule(@RequestBody EntranceRuleBean entranceRuleBean) {
        System.out.println(entranceRuleBean+"这是添加准入规则信息");
        entranceRuleBean.setDelFlag(0);
        ResultMsg resultMsg = new ResultMsg(200, "新增成功", null);
        System.out.println(entranceRuleBean);
       entranceRuleServiceImpl.addEntranceRule(entranceRuleBean);
        return resultMsg;
    }

    /**
     * 修改准入规则
     * @param entranceRuleBean
     * @return
     */
    @PutMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(@RequestBody EntranceRuleBean  entranceRuleBean){
        System.out.println(entranceRuleBean+"-------------修改部门信息");
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        int row=entranceRuleServiceImpl.updateEntranceRule(entranceRuleBean);
        if(row == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg deleteRule(@PathVariable("id") Long id){
        System.out.println("这是删除准则controller"+id);
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null) {
           entranceRuleServiceImpl.deleteEntranceRule(id.intValue());
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    /**
     * 修改准则状态
     * @param id
     * @param useStatus
     * @return
     */
    @PutMapping(value = "/status/{id}/{status}", produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id, @PathVariable("status") int useStatus) {
        ResultMsg res = new ResultMsg(200, "操作成功", null);
        System.out.println(id);
        System.out.println(useStatus);
        if (id != null ) {
           EntranceRuleBean entranceRuleBean=new EntranceRuleBean();
           entranceRuleBean.setId(id);
           entranceRuleBean.setUseStatus(useStatus);
           entranceRuleServiceImpl.updateEntranceRule(entranceRuleBean);
            return res;
        } else {
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
}
